package com.xsm.ppsecret.presenter;

import android.util.Base64;

import com.xsm.ppsecret.ui.implview.IRegistActivity;
import com.xsm.ppsecret.util.CryptUtil;
import com.xsm.ppsecret.util.NativeHelper;
import com.xsm.ppsecret.util.ShareManager;

/**
 * Author: Xiasem
 * Email: xiasem@163.com
 * Date: 17-4-13.
 */

public class RegistPresenter extends BasePresenter{

    private IRegistActivity mRegistActivity;

    public RegistPresenter(IRegistActivity registActivity) {
        mRegistActivity = registActivity;
    }

    /**
     * 注册并进行校验
     * @param root1 第一次输入的管理员密码
     * @param root2 第二次输入的管理员密码
     * @param one1 第一次输入的一级密码
     * @param one2 第二次输入的一级密码
     * @param two1 第一次输入的二级密码
     * @param two2 第二次输入的二级密码
     * @param question 密码保护问题
     * @param answer 密码保护答案
     */
    public void regist(String root1, String root2, String one1, String one2, String two1, String two2, String question, String answer) {
        if (root1 == null || root1.isEmpty()) {
            mRegistActivity.verifyError("请输入超级用户密码");
            return;
        }
        if (root2 == null || root2.isEmpty()) {
            mRegistActivity.verifyError("请再次输入超级用户密码");
            return;
        }
        if (!root1.equals(root2)) {
            mRegistActivity.verifyError("两次输入的超级用户密码不一致");
            return;
        }
        if (one1 == null || one1.isEmpty()) {
            mRegistActivity.verifyError("请输入一级用户密码");
            return;
        }
        if (one2 == null || one2.isEmpty()) {
            mRegistActivity.verifyError("请再次输入一级用户密码");
            return;
        }
        if (!one1.equals(one2)) {
            mRegistActivity.verifyError("两次输入的一级用户密码不一致");
            return;
        }
        if (two1 == null || two1.isEmpty()) {
            mRegistActivity.verifyError("请输入二级用户密码");
            return;
        }
        if (two2 == null || two2.isEmpty()) {
            mRegistActivity.verifyError("请再次输入二级用户密码");
            return;
        }
        if (!two1.equals(two2)) {
            mRegistActivity.verifyError("两次输入的二级用户密码不一致");
            return;
        }
        if (question == null || question.isEmpty()) {
            mRegistActivity.verifyError("请输入密码保护问题");
            return;
        }
        if (answer == null || answer.isEmpty()) {
            mRegistActivity.verifyError("请输入密码保护问题答案");
            return;
        }
        ShareManager shareManager = ShareManager.getInstance();
        byte[] rootBytes = CryptUtil.aesEncryptWithIv(root1.getBytes(), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String root = Base64.encodeToString(rootBytes, Base64.NO_WRAP);
        shareManager.setPasswordRoot(root);

        byte[] oneBytes = CryptUtil.aesEncryptWithIv(one1.getBytes(), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String one = Base64.encodeToString(oneBytes, Base64.NO_WRAP);
        shareManager.setPasswordOne(one);

        byte[] twoBytes = CryptUtil.aesEncryptWithIv(two1.getBytes(), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String two = Base64.encodeToString(twoBytes, Base64.NO_WRAP);
        shareManager.setPasswordTwo(two);

        byte[] questionBytes = CryptUtil.aesEncryptWithIv(question.getBytes(), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String questionCrypt = Base64.encodeToString(questionBytes, Base64.NO_WRAP);
        shareManager.setEncryptedQuestion(questionCrypt);

        byte[] answerBytes = CryptUtil.aesEncryptWithIv(answer.getBytes(), NativeHelper.getAesKey(), NativeHelper.getAesIvKey());
        String answerCrypt = Base64.encodeToString(answerBytes, Base64.NO_WRAP);
        shareManager.setEncryptedAnswer(answerCrypt);
        shareManager.setRegistTrue();
        mRegistActivity.verifyOk();
    }

}
